package com.bbc.fragment;

import android.os.Handler;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.Toast;

import com.bbc.R;
import com.bbc.adapter.HomeAdapter;
import com.bbc.base.BaseFragment;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;


/**
 * Created by Administrator on 2017/9/26.
 */

public class HomeFragment extends BaseFragment {
    @BindView(R.id.recycleView)
    RecyclerView mRecycleView;
    @BindView(R.id.refreshLayou)
    SwipeRefreshLayout mRefreshLayou;
    private List<String> mList;
    private HomeAdapter mAdapter;
    private int page = 1;
    private LinearLayoutManager mLinearLayoutManager;

    @Override
    protected int getViewLayou() {
        return R.layout.fragment_recycleview;
    }


    @Override
    protected void initData() {
        //s设置刷新的是颜色的状态
        mRefreshLayou.setColorSchemeResources(R.color.reda, R.color.reda1, R.color.grade, R.color.colorPrimary);
        mLinearLayoutManager = new LinearLayoutManager(getContext());
        mRecycleView.setLayoutManager(mLinearLayoutManager);
        mList = new ArrayList<>();
        for (int i = 0; i < 10; i++) {
            mList.add("我是测试数据" + i);
        }
        mAdapter = new HomeAdapter(getContext(), mList);
        mRecycleView.setAdapter(mAdapter);
    }

    @Override
    protected void findViewId() {

    }

    @Override
    protected void setListeners() {
        //下拉刷新最新数据
        mRefreshLayou.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                new Handler().postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        List<String> newDatas = new ArrayList<String>();
                        for (int i = 0; i < 5; i++) {
                            int index = i + 1;
                            newDatas.add("new item" + index);
                        }
                        mAdapter.initDatas(newDatas);
                        Toast.makeText(getContext(), "更新了五条数据...", Toast.LENGTH_SHORT).show();
                        mRefreshLayou.setRefreshing(false);
                    }
                }, 2000);
            }
        });
        mRecycleView.addOnScrollListener(new RecyclerView.OnScrollListener() {

            private int mLastVisibleItemPosition;

            @Override
            public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
                super.onScrollStateChanged(recyclerView, newState);
                if (newState == RecyclerView.SCROLL_STATE_IDLE && mLastVisibleItemPosition + 1 == mAdapter.getItemCount()) {
                    new Handler().postDelayed(new Runnable() {
                        @Override
                        public void run() {
                            List<String> newDatas = new ArrayList<String>();
                            for (int i = 0; i < 5; i++) {
                                int index = i + 1;
                                newDatas.add("more item" + index);
                            }
                            mAdapter.addData(newDatas);
                        }
                    }, 2000);
                }
            }

            @Override
            public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
                super.onScrolled(recyclerView, dx, dy);
                mLastVisibleItemPosition = mLinearLayoutManager.findLastVisibleItemPosition();
            }
        });

    }
}
